package com.kung.qvs.ui.main.fragment;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.kung.qvs.data.JtRepository;
import com.kung.qvs.entity.PropInfoBean;
import com.kung.qvs.entity.database.SqlManager;
import com.kung.qvs.utils.Constants;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class PwdForgetModel extends BaseViewModel<JtRepository> {

    public ObservableField<String> mPwd = new ObservableField<>();
    public ObservableField<String> mPwdNew1 = new ObservableField<>();
    public ObservableField<String> mPwdNew2 = new ObservableField<>();

    private PropInfoBean propInfoBean1; //下载的公司信息
    private PropInfoBean propInfoBean2; //本地保存的公司信息
    private String mCompanyName = "";

    public PwdForgetModel(@NonNull Application application, JtRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //下拉刷新完成
        public SingleLiveEvent eventToBackPage = new SingleLiveEvent<>();
        public SingleLiveEvent<String> eventChangeTrashType = new SingleLiveEvent<>();
    }

    public BindingCommand onCommandResetPwd = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //修改密码
            if (StringUtils.isTrimEmpty(mPwd.get())) {
                ToastUtils.showShort("请输入企业名称");
                return;
            }

            if (StringUtils.isTrimEmpty(mPwdNew1.get()) || StringUtils.isTrimEmpty(mPwdNew2.get())) {
                ToastUtils.showShort("请输入新密码");
                return;
            }

            if (StringUtils.isTrimEmpty(mCompanyName)) {
                ToastUtils.showShort("暂无企业信息");
                return;
            } else {
                if (!mPwd.get().equals(mCompanyName)) {
                    ToastUtils.showShort("企业信息错误");
                    return;
                }
            }

            if (!mPwdNew1.get().equals(mPwdNew2.get())) {
                ToastUtils.showShort("两次新密码不一致");
                return;
            }

            model.savePassword(mPwdNew1.get());
            ToastUtils.showShort("密码修改成功！");
        }
    });

    public BindingCommand onCommandBack = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.eventToBackPage.call();
        }
    });

    @Override
    public void onCreate() {
        super.onCreate();
        loadLocalCompanyInfo();
    }

    private void loadLocalCompanyInfo() {
        propInfoBean1 = SqlManager.queryPropInfoBeanById(Constants.LOCAL_CODE_DEFAULT);
        propInfoBean2 = SqlManager.queryPropInfoBeanById(Constants.LOCAL_CODE_USER);
        if (propInfoBean1 != null) {
            mCompanyName = propInfoBean1.getProducer();
        }
    }
}
